package com.pubinfo.passbook.common.entity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.pubinfo.passbook.common.handler.rabbitmq.*;
import com.pubinfo.passbook.common.model.bo.rabbitmq.*;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

/**
 * t_4a_person
 *
 * @TableName t_4a_person
 */
@TableName(value = "t_rabbitmq_person", autoResultMap = true)
@Data
@NoArgsConstructor
public class TRabbitmqPerson implements Serializable {
  /**
   * 33035209
   */

  private String acct;
  
  /**
   * update
   */
  private String handle;
  
  /**
   * 202110220ea5984bbc8b42cc9f3f96b233d55b0f
   */
  private String serial;
  
  /**
   * person
   */
  private String type;
  
  /**
   * 11181001
   */
  @TableId(value = "acct_id")
  private Integer acctId;
  /**
   * 2
   */
  private String acctType;
  
  /**
   * 2099-01-01 00:00:00
   */
  private String disableDate;
  
  /**
   * 2019-01-01 00:00:00
   */
  private String enableDate;
  
  /**
   * 0DB7DBB1F7EAF44CF5C077C9BC699A35
   */
  private String password;
  
  /**
   * 0DB7DBB1F7EAF44CF5C077C9BC699A35
   */
  private String symmetryPassword;
  
  /**
   * 1314
   */
  private String salt;
  
  /**
   * 1
   */
  private String source;
  
  /**
   *
   */
  @TableField(typeHandler = TbAcctCrossRelListTypeHandler.class)
  private List<TbAcctCrossRel> tbAcctCrossRel;
  
  /**
   *
   */
  @TableField(typeHandler = TbAcctOrgRelListTypeHandler.class)
  private List<TbAcctOrgRel> tbAcctOrgRel;
  
  /**
   *
   */
  @TableField(typeHandler = FastjsonTypeHandler.class)
  private JSONObject tbSourceVo;
  /**
   * 人员
   */
  @TableField(typeHandler = FastjsonTypeHandler.class)
  private JSONObject tbPersonnel;
  
  @TableField(typeHandler = FastjsonTypeHandler.class)
  private JSONObject context;
  /**
   * 联系方式tbContact
   */
  @TableField(typeHandler = TbContactListTypeHandler.class)
  private List<TbContact> tbContact;
  
  /**
   * 教育经历
   */
  @TableField(typeHandler = TbEduListTypeHandler.class)
  private List<TbEdu> tbEdu;
  
  /**
   * 家庭成员
   */
  @TableField(typeHandler = TbFamilyListTypeHandler.class)
  private List<TbFamily> tbFamily;
  
  /**
   * 证件信息
   */
  @TableField(typeHandler = TbCertListTypeHandler.class)
  private List<TbCert> tbCert;
  
  @TableField(typeHandler = TbPerRLOrgRelListTypeHandler.class, value = "tb_per_rl_org_rel")
  private List<TbPerRLOrgRel> tbPerRLOrgRel;
  @TableField(typeHandler = HrPostListTypeHandler.class)
  private List<HrPost> hrPost;
  /**
   * 工作履历
   */
  @TableField(typeHandler = TbPsnjobListTypeHandler.class)
  private List<TbPsnjob> tbPsnjob;
  
  @TableField(typeHandler = HrRoleListTypeHandler.class)
  private List<HrRole> hrRole;
  /**
   * 状态
   */
  private String statusCd;
  
  /**
   * 创建时间
   */
  @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
  private Date createDate;
  
  /**
   * 创建人
   */
  private Integer createUser;
  
  /**
   * 修改时间
   */
  @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
  private Date updateDate;
  
  /**
   * 修改人
   */
  private Integer updateUser;
  
  /**
   * 状态时间
   */
  @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
  private Date statusDate;
  
  /**
   * 2
   */
  private String userHostType;
  
  /**
   * 同步时间
   */
  @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
  @TableField(fill = FieldFill.INSERT)
  private Date createTime;
  
  /**
   * 修改时间
   */
  @TableField(fill = FieldFill.UPDATE)
  @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
  private Date updateTime;

  /**
   * 公司代码
   */
  private String orgCode;
  /**
   * 部门代码
   */
  private String departmentCode;
  /**
   * 电话号码
   */
  private String phone;
  
  @TableField(exist = false)
  private static final long serialVersionUID = 1L;
  
  
  @SneakyThrows
  public TRabbitmqPerson(RabbitMqRoot root) {
    BeanUtils.copyProperties(root, this);
    BeanUtils.copyProperties(root.getContext(), this);
    Context context = root.getContext();
    String createDate = context.getCreateDate();
    String updateDate = context.getUpdateDate();
    String statusDate = context.getStatusDate();
    this.context = JSONObject.parseObject(JSON.toJSONString(context));
    if (StringUtils.isNotBlank(createDate)) {
      this.createDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(createDate);
    }
    if (StringUtils.isNotBlank(createDate)) {
      this.updateDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(updateDate);
    }
    if (StringUtils.isNotBlank(statusDate)) {
      this.statusDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(statusDate);
    }

    //提取出手机号码
    for(TbContact tbContact:this.tbContact)
    {
      if("1".equals(tbContact.getContactType()))
      {
        this.phone=tbContact.getContent();
      }
    }

    //提取出公司号码和部门号码
    int max=0;
    for(TbPerRLOrgRel tbPerRLOrgRel: this.tbPerRLOrgRel)
    {
      int count=0;
      String s=tbPerRLOrgRel.getFullOrgId();
      for(int i=0;i<s.length();i++){
        if(s.charAt(i)=='/'){
          count++;
        }
      }
      //取分隔符数量最大的字符串
      if(count>max)
      {
        this.orgCode=s.substring(s.lastIndexOf('/')+1);
        this.departmentCode=s.substring(s.lastIndexOf('/',s.lastIndexOf('/')-1)+1,s.lastIndexOf('/'));
        max=count;
      }
    }

  }
  
}